package com.hutiao.snowhb.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hutiao.snowhb.common.DateTimeUtil;
import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.dao.BaseDao;
import com.hutiao.snowhb.entities.PhotoCommentBean;
import com.hutiao.snowhb.entities.PhotographerBean;
import com.hutiao.snowhb.entities.WeixinUserInfo;

@Component
public class PhotographerService {
	
	private static Logger logger = Logger.getLogger(PhotographerService.class);

	@Autowired
	private BaseDao baseDao;

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	

	public int saveUserPhoto(PhotographerBean bean) throws SQLException {
		
		String sql = "insert into hd_photographer(openId,photoUrl,photoTitle,photoDesc,mediaId,createTime,updateTime) "
				+ "values('"+bean.getOpenId()+"','"+bean.getPhotoUrl()+"','"+bean.getPhotoTitle()+"',"
						+ "'"+bean.getPhotoDesc()+"','"+bean.getMediaId()+"',now(),now());";
		
		return this.baseDao.save(sql);
		
	}
	
	public List<PhotographerBean> getPhotoFlow(int lastId) {
		List<PhotographerBean> list = new ArrayList<PhotographerBean>();
		
		String w = "";
		if (lastId != -1) {
			w = " and id < '"+lastId+"' ";
		}
		
		String sql = "select id,openId,photoUrl,photoTitle,photoDesc,mediaId,ranking,saveFull,saveThumb,`status`,likeCount,commentCount,createTime "
				+ "from hd_photographer where 1=1 and status=1 " + w
				+ "order by id desc limit 18";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		for (Map<String, Object> temp : tempList) {
			PhotographerBean bean = new PhotographerBean();
			bean.setId(Integer.parseInt(temp.get("id").toString()));
			bean.setOpenId(temp.get("openId").toString());
			bean.setPhotoUrl(temp.get("photoUrl").toString());
			bean.setPhotoTitle(temp.get("photoTitle").toString());
			bean.setPhotoDesc(temp.get("photoDesc").toString());
			bean.setMediaId(temp.get("mediaId").toString());
			bean.setRanking(Integer.parseInt(temp.get("ranking").toString()));
			bean.setSaveFull(temp.get("saveFull")==null?null:temp.get("saveFull").toString());
			bean.setSaveThumb(temp.get("saveThumb")==null?null:temp.get("saveThumb").toString());
			bean.setStatus(Integer.parseInt(temp.get("status").toString()));
			bean.setLikeCount(Integer.parseInt(temp.get("likeCount").toString()));
			bean.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
			bean.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
			list.add(bean);
		}
		
		return list;
	}
	
	
	public List<PhotographerBean> getPhotoAlbum() {
		
		List<PhotographerBean> list = new ArrayList<PhotographerBean>();
		
		String sql = "select id,openId,photoUrl,photoTitle,photoDesc,mediaId,ranking,saveFull,saveThumb,`status`,likeCount,commentCount,createTime "
				+ "from hd_photographer where status=1 order by id desc limit 18";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		for (Map<String, Object> temp : tempList) {
			PhotographerBean bean = new PhotographerBean();
			bean.setId(Integer.parseInt(temp.get("id").toString()));
			bean.setOpenId(temp.get("openId").toString());
			bean.setPhotoUrl(temp.get("photoUrl").toString());
			bean.setPhotoTitle(temp.get("photoTitle").toString());
			bean.setPhotoDesc(temp.get("photoDesc").toString());
			bean.setMediaId(temp.get("mediaId").toString());
			bean.setRanking(Integer.parseInt(temp.get("ranking").toString()));
			bean.setSaveFull(temp.get("saveFull")==null?null:temp.get("saveFull").toString());
			bean.setSaveThumb(temp.get("saveThumb")==null?null:temp.get("saveThumb").toString());
			bean.setStatus(Integer.parseInt(temp.get("status").toString()));
			bean.setLikeCount(Integer.parseInt(temp.get("likeCount").toString()));
			bean.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
			bean.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
			list.add(bean);
		}
		
		return list;
	}
	
	
	public PhotographerBean getPhotoById(int id) throws Exception {
		
		String sql = "select id,openId,photoUrl,photoTitle,photoDesc,mediaId,ranking,saveFull,saveThumb,`status`,likeCount,commentCount,createTime "
				+ "from hd_photographer where id='"+id+"'";
		
		Map<String, Object> temp = this.baseDao.queryForMap(sql);
		
		if (temp == null) {
			return null;
		}
		
		PhotographerBean bean = new PhotographerBean();
		bean.setId(Integer.parseInt(temp.get("id").toString()));
		bean.setOpenId(temp.get("openId").toString());
		bean.setPhotoUrl(temp.get("photoUrl").toString());
		bean.setPhotoTitle(temp.get("photoTitle").toString());
		bean.setPhotoDesc(temp.get("photoDesc").toString());
		bean.setMediaId(temp.get("mediaId").toString());
		bean.setRanking(Integer.parseInt(temp.get("ranking").toString()));
		bean.setSaveFull(temp.get("saveFull")==null?null:temp.get("saveFull").toString());
		bean.setSaveThumb(temp.get("saveThumb")==null?null:temp.get("saveThumb").toString());
		bean.setStatus(Integer.parseInt(temp.get("status").toString()));
		bean.setLikeCount(Integer.parseInt(temp.get("likeCount").toString()));
		bean.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
		bean.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
		
		WeixinUserInfo user = WeixinUtil.getUserInfo(bean.getOpenId());
		bean.setUser(user);
		
		return bean;
	}
	
	public List<PhotoCommentBean> getComment(int pid) throws Exception {
		
		List<PhotoCommentBean> pcBeanList = new ArrayList<PhotoCommentBean>();
		
		String sql = "select cid,pid,openId,type,comment,createTime from hd_photographer_comment where pid='"+pid+"'";
		
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		for (Map<String, Object> temp : tempList) {
			PhotoCommentBean bean = new PhotoCommentBean();
			bean.setCid(Integer.parseInt(temp.get("cid").toString()));
			bean.setPid(Integer.parseInt(temp.get("pid").toString()));
			bean.setOpenId(temp.get("openId").toString());
			bean.setType(Integer.parseInt(temp.get("type").toString()));
			bean.setComment(temp.get("comment").toString());
			bean.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
			
			WeixinUserInfo user = WeixinUtil.getUserInfo(bean.getOpenId());
			bean.setUser(user);
			
			pcBeanList.add(bean);
		}
		
		return pcBeanList;
	}
	
	public boolean comment(int pid, String openId, int type, String Comment) {
		
		String sql = "insert into hd_photographer_comment(pid,openId,type,comment,createTime) "
				+ "values ("+pid+", '"+openId+"', "+type+", '"+Comment+"', now());";
		
		try {
			this.baseDao.execute(sql);
		} catch(Exception e) {
			logger.error("Exception", e);
			return false;
		}
		
		return true;
	}
	
	public boolean deleteComment(int cid) {
		
		String sql = "delete from hd_photographer_comment where cid="+cid;
		
		try {
			this.baseDao.execute(sql);
		} catch(Exception e) {
			logger.error("Exception", e);
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * 获取照片总数
	 * @param status 照片的状态： 0-未审核 1-审核 2-删除 -1-不过滤
	 * @return
	 */
	public int getPhotosCount(int status) {
		
		String sql = "select count(*) from hd_photographer ";
		if (status != -1) {
			sql += "where status="+status+" ";
		}
		
		int c = this.baseDao.queryForInt(sql);
		return c;
	}
	
	
	/**
	 * 分页查询用户上传的照片
	 * @param pageNo 页面编号
	 * @param n 页面的照片数量
	 * @param status 照片的状态： 0-未审核 1-审核 2-删除 -1-不过滤
	 * @return
	 */
	public List<PhotographerBean> getPhotosByPage(int pageNo, int n, int status) {
		
		int m = (pageNo - 1) * n;
		
		String strCase = "";
		if (status != -1) {
			strCase = "and status="+status+" ";
		}
		
		String sql = "select id,openId,photoUrl,photoTitle,photoDesc,mediaId,ranking,saveFull,saveThumb,`status`,likeCount,commentCount,createTime "
				+ "from hd_photographer where 1=1 " + strCase + " "
				+ "order by id desc limit " + m + ", " + n + "";
		
		
		List<PhotographerBean> list = new ArrayList<PhotographerBean>();
		List<Map<String, Object>> tempList = this.baseDao.queryForList(sql);
		
		if (tempList == null || tempList.size() == 0) {
			return null;
		}
		
		for (Map<String, Object> temp : tempList) {
			PhotographerBean bean = new PhotographerBean();
			bean.setId(Integer.parseInt(temp.get("id").toString()));
			bean.setOpenId(temp.get("openId").toString());
			bean.setPhotoUrl(temp.get("photoUrl").toString());
			bean.setPhotoTitle(temp.get("photoTitle").toString());
			bean.setPhotoDesc(temp.get("photoDesc").toString());
			bean.setMediaId(temp.get("mediaId").toString());
			bean.setRanking(Integer.parseInt(temp.get("ranking").toString()));
			bean.setSaveFull(temp.get("saveFull")==null?null:temp.get("saveFull").toString());
			bean.setSaveThumb(temp.get("saveThumb")==null?null:temp.get("saveThumb").toString());
			bean.setStatus(Integer.parseInt(temp.get("status").toString()));
			bean.setLikeCount(Integer.parseInt(temp.get("likeCount").toString()));
			bean.setCommentCount(Integer.parseInt(temp.get("commentCount").toString()));
			bean.setCreateTime(DateTimeUtil.String2Date2(temp.get("createTime").toString()));
			list.add(bean);
		}
		
		return list;
	}
	
}
